package graphs;

//Used for generating different colors for each graphic
import java.awt.Color;

//Enables the graph to be returned as a panel
import javax.swing.JPanel;

//Needed for reading measurements
import mechanics.Meting;

// JFreeChart imports
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/**
 * Graph class is used for creating the graphical views
 * 
 * @version 31-03-2009
 */
public class Graph {

	// The panel where the chart is being created in
	private ChartPanel chartPanel;
	// The chart, which shows the data
	private JFreeChart chart;
	// Dataset for light
	private TimeSeriesCollection licht_dataset;
	// Dataset for temperature
	private TimeSeriesCollection temperatuur_dataset;
	// Dataset for humidity
	private TimeSeriesCollection vochtigheid_dataset;
	// Serie of measurements of light
	private TimeSeries licht_series;
	// Serie of measurements of temperature
	private TimeSeries temperatuur_series;
	// Serie of measurements of humidity
	private TimeSeries vochtigheid_series;
	// Holds the title of the graph
	private String chartTitle;

	/**
	 * Constructor makes the series and sets the maximum amount of items
	 * 
	 * @param maxitems
	 */
	public Graph(String title, int maxitems) {
		// Set the title
		this.chartTitle = title;

		// Serie for light
		this.licht_series = new TimeSeries("Licht", Second.class);
		// Serie for temperature
		this.temperatuur_series = new TimeSeries("Temperatuur", Second.class);
		// Serie for humidity
		this.vochtigheid_series = new TimeSeries("Vochtigheid", Second.class);

		// Maximum amount of items for light
		this.licht_series.setMaximumItemCount(maxitems);
		// Maximum amount of items for temperature
		this.temperatuur_series.setMaximumItemCount(maxitems);
		// Maximum amount of items for humidity
		this.vochtigheid_series.setMaximumItemCount(maxitems);
	}

	/**
	 * Adds a measurement to the series
	 * 
	 * @param second Time of the measurement
	 * @param meting The actual measurement
	 */
	public void addMeting(Second second, Meting meting) {
		// Add the measurement for light
		licht_series.add(second, meting.getLight());
		// Add the measurement for temperature
		temperatuur_series.add(second, meting.getTemperature());
		// Add the measurement for humidity
		vochtigheid_series.add(second, meting.getHumidity());
	}

	/**
	 * Creates and returns the graph based upon the given data
	 * 
	 * @return A graph based on specified data
	 */
	public JPanel getGraph() {
		// Create the dataset for light
		this.licht_dataset = new TimeSeriesCollection(licht_series);
		// Create the dataset for temperature
		this.temperatuur_dataset = new TimeSeriesCollection(temperatuur_series);
		// Create the dataset for humidity
		this.vochtigheid_dataset = new TimeSeriesCollection(vochtigheid_series);

		// Create the chart
		this.chart = ChartFactory.createTimeSeriesChart(chartTitle, "Tijd", "Waarde", licht_dataset, true, true, false);

		// Get the plot of the chart
		XYPlot plot = chart.getXYPlot();

		// Add dataset for temperature and set the default renderer
		plot.setDataset(1, temperatuur_dataset);
		plot.setRenderer(1, new StandardXYItemRenderer());
		// Add dataset for humidity and set the default renderer
		plot.setDataset(2, vochtigheid_dataset);
		plot.setRenderer(2, new StandardXYItemRenderer());

		// Get the renderer and change the colors
		XYItemRenderer renderer = plot.getRenderer();
		renderer.setSeriesPaint(0, Color.green);
		renderer.setSeriesPaint(2, Color.red);

		// Create a panel for the chart and add mouse functionality
		this.chartPanel = new ChartPanel(chart);
		this.chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		this.chartPanel.setMouseZoomable(true, false);

		// Return the chart as a panel
		return this.chartPanel;
	}

	/**
	 * Prints the chart as a new window
	 */
	public void printChart() {
		// Create the graph
		this.getGraph();

		// Make a frame for the chart
		ChartFrame frame = new ChartFrame("First", chart);
		// Set the size and print the frame
		frame.pack();
		frame.setVisible(true);
	}

}